package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration // 标记为配置类，Spring会自动扫描并加载
public class ThreadPoolConfig {

    // 定义线程池Bean，名称为"taskExecutor"，供其他类注入使用
    @Bean(name = "taskExecutor")
    public ExecutorService taskExecutor() {
        // 创建线程池实例，参数说明：
        // 核心线程数5，最大线程数10，空闲线程存活时间60秒
        // 任务队列容量20，超出后由调用者线程执行（CallerRunsPolicy）
        return new ThreadPoolExecutor(
                5,
                10,
                60L,
                java.util.concurrent.TimeUnit.SECONDS,
                new java.util.concurrent.LinkedBlockingQueue<>(20),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }
}
